package com.quiz.activity;


import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.bugsense.trace.BugSenseHandler;
import com.flurry.android.FlurryAgent;
import com.quiz.activity.util.QuizUtil;
import com.quiz.dao.PlayerDatasource;
import com.quiz.model.Player;

public class HomeQuizActivity extends Activity {

    private PlayerDatasource dataSource;
    private Player player;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        BugSenseHandler.setup(this, "c53c1959");
        FlurryAgent.onStartSession(this, QuizUtil.FLURRY_KEY);

        dataSource = new PlayerDatasource(this);
        dataSource.open();


        setContentView(R.layout.home);
        TextView logo = (TextView) findViewById(R.id.logoText);
        QuizUtil.setMafiaFont(logo, 48, getAssets());

        Button playnowButton = (Button) findViewById(R.id.playButton);
        playnowButton.setOnClickListener(new PlaynowClickListener(this, true));

        Button leaderBoardButton = (Button) findViewById(R.id.leaderboardsButton);
        leaderBoardButton.setOnClickListener(new ViewLeaderboardsClickListener(this));

        Button settingsButton = (Button) findViewById(R.id.settingsButton);
        settingsButton.setOnClickListener(new ViewSettingsClickListener(this));

        Button howToPlayButton = (Button) findViewById(R.id.instructionsButton);
        howToPlayButton.setOnClickListener(new HowToPlayClickListener(this));

        Button feedbackButton = (Button) findViewById(R.id.feedbackButton);
        feedbackButton.setOnClickListener(new ViewFeedbackClickListener(this));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onPause() {
        dataSource.close();
        super.onPause();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onResume() {
        dataSource.open();
        super.onResume();

        player = dataSource.getPlayerDetails();
        TextView rankingText = (TextView) findViewById(R.id.rankingText);
        QuizUtil.setMafiaFont(rankingText, 32, getAssets());
        rankingText.setText(player.getCurrentRank());

        if (player.getHighScore() > 0) {
            TextView highscoreText = (TextView) findViewById(R.id.highscoreText);
            highscoreText.setText(QuizUtil.formatScore(player.getHighScore()) + " points");
        }
    }


    /**
     * {@inheritDoc}
     */
    @Override
    protected void onStop() {
        super.onStop();
        FlurryAgent.onEndSession(this);
    }

    public Player getPlayer() {
        return player;
    }
}
